package development;

import java.util.Vector;

import state.GameConstants;
import state.GameGlobal;
import state.Graphics;
import state.Manager;
import state.TimerListener;
import state.component.TextField;
import state.component.TextFieldStyle;
import state.font.FontManager;
import state.font.ImageText;
import state.util.GameColor;
import state.util.TwoDimensionInt;
import development.bean.ChatInfo;
import development.bean.ChatInfoArray;
import development.bean.User;

public class ChatComponent implements TimerListener {
  public static int MAX_CHAT_BOX_WIDTH = 80;
  
  private ChatInfoArray chatInfos;
  
  private ImageText text8;
  
  private int messageShowTime = 50; // 5 s
  private int maxMessageLength = 30;
  
  private String currentRoomId = "";
  
  private int textColor = GameColor.WHITE_CODE;
  
  private int backGroundColor = 0x3CA446;
  
  private int borderColor = GameColor.WHITE_CODE;
  
  private TwoDimensionInt chatLocations;
  
  private TextField chatTextField;
  
  private int inputMessageDelayCount;
  
  public ChatComponent() {
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    setTimerListener();
  }
  
  public void init() {
    inputMessageDelayCount = -1;
    if (chatTextField != null) {
      chatTextField.clearText();
    }
    
    if (chatInfos != null) {
      for (int i = 0; i < chatInfos.len(); i++) {
        if (chatInfos.array[i] != null) {
          chatInfos.array[i].clear();
        }
      }
    }
  }
  
  public void setInputChatMessageTextField(TextFieldStyle style, int x, int y, int width, int height) {
    Manager manager = Global.systemCanvas.frmCurrent.getManager();
    if (chatTextField == null) {
      chatTextField = new TextField(manager, TextField.CONSTRAINT_ANY, null);
    } else {
      if (chatTextField.getManager() != null) {
        chatTextField.getManager().remove(chatTextField);
      }
      chatTextField.setManager(manager);
    }
    chatTextField.setSize(width, height);
    chatTextField.setPosition(x, y);
    chatTextField.setFocused(true);
    chatTextField.setVisible(false);
  }
  
  public boolean isShowInputChatMessageTextField() {
    if (chatTextField == null) {
      return false;
    }
    return chatTextField.isVisible();
  }

  public void setShowInputChatMessageTextField(boolean isShowInputChatMessageTextField) {
    if (chatTextField == null) {
      return;
    }
    chatTextField.setVisible(isShowInputChatMessageTextField);
    if (!chatTextField.isVisible()) {
      inputMessageDelayCount = -1;
      chatTextField.clearText();
    } else {
      inputMessageDelayCount = 0;
    }
  }
  
  private void setTimerListener() {
    GameGlobal.systemCanvas.getTimer().addTarget(this);
  }
  
  public void updateChatList(Vector users) {
    ChatInfoArray oldChatInfos = chatInfos;
    chatInfos = ChatInfoArray.create(users.size());
    
    for (int i = 0; i < users.size(); i++) {
      User user = (User) users.elementAt(i);
      ChatInfo chatInfo = getChatInfoByUser(oldChatInfos, user.getName());
      if (chatInfo != null) {
        chatInfos.array[i] = chatInfo;
      } else {
        chatInfos.array[i] = new ChatInfo(user.getName(), maxMessageLength);
      }
    }
  }
  
  public void initChatLocation(TwoDimensionInt chatLocations1) {
    this.chatLocations = chatLocations1;
  }
  
  private ChatInfo getChatInfoByUser(ChatInfoArray chatInfoList, String username) {
    if (chatInfoList != null) {
      for (int i = 0; i < chatInfoList.len(); i++) {
        if (chatInfoList.array[i] != null && chatInfoList.array[i].getFromUser().equals(username)) {
          return chatInfoList.array[i];
        }
      }
    }
    return null;
  }
  
  public void setMessageShowTime(int time) {
    this.messageShowTime = time;
  }
  
  public int getMaxMessageLength() {
    return maxMessageLength;
  }

  public void setMaxMessageLength(int maxMessageLength1) {
    this.maxMessageLength = maxMessageLength1;
  }
  
  public String getCurrentRoomId() {
    return currentRoomId;
  }

  public void setCurrentRoomId(String currentRoomId1) {
    this.currentRoomId = currentRoomId1;
  }
  
  public void addMessage(String ofUserName, String message) {
    ChatInfo chatInfo = getChatInfoByUser(chatInfos, ofUserName);
    if (chatInfo == null) {
      return;
    }
    chatInfo.appentMessage(message);
    chatInfo.timeCount = 0;
  }
  
  public int getTextColor() {
    return textColor;
  }

  public void setTextColor(int textColor1) {
    this.textColor = textColor1;
  }
  
  public int getBackGroundColor() {
    return backGroundColor;
  }

  public void setBackGroundColor(int backGroundColor1) {
    this.backGroundColor = backGroundColor1;
  }

  public int getBorderColor() {
    return borderColor;
  }

  public void setBorderColor(int borderColor1) {
    this.borderColor = borderColor1;
  }

  public void draw(Graphics g) {
    for (int i = 0; i < chatInfos.len(); i++) {
      if ((chatInfos.array[i] != null) && chatInfos.array[i].getMessage().length() > 0 && chatLocations.getHeight() > i) {
        int chatBoxWidth = chatInfos.array[i].getLineMaxLength() + 5;
        int bottomY = text8.drawParagraph1(g, chatInfos.array[i].getMessage(), textColor, chatBoxWidth, 1, chatLocations.get(i, 0) + 6, chatLocations.get(i, 1));
        g.setColor(backGroundColor);
        g.fillRect(chatLocations.get(i, 0), chatLocations.get(i, 1), chatBoxWidth + 5, bottomY - chatLocations.get(i, 1) + 5);
        text8.drawParagraph1(g, chatInfos.array[i].getMessage(), textColor, chatBoxWidth, 1, chatLocations.get(i, 0) + 6, chatLocations.get(i, 1));
        g.setColor(borderColor);
        g.drawRect(chatLocations.get(i, 0), chatLocations.get(i, 1), chatBoxWidth + 5, bottomY -chatLocations.get(i, 1) + 5);
      }
    }
    
    if (chatTextField != null && chatTextField.isVisible()) {
      g.setColor(0xA9A9A9);
      g.fillRect(0, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 40, GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 40);
      g.drawRect(0, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 40, GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 40);
      text8.drawString2(g, "Nội dung:", GameColor.WHITE_CODE, 5, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 25, GameConstants.BOTTOM_LEFT_ANCHOR);
      chatTextField.draw(g);
    }
  }
  
  public void keyReleased(int keyCode) {
    if (chatTextField != null) {
      chatTextField.keyReleased(null, keyCode);
    }
  }
  
  public TextField getChatTextField() {
    return chatTextField;
  }

  public void doTask() {
    if (chatInfos != null) {
      for (int i = 0; i < chatInfos.len(); i++) {
        if (chatInfos.array[i] != null) {
          if (chatInfos.array[i].timeCount > -1) {
            chatInfos.array[i].timeCount++;
          }
          
          if (chatInfos.array[i].timeCount > messageShowTime) {
            chatInfos.array[i].setMessage("");
            chatInfos.array[i].timeCount = -1;
          }
        }
      }
    }
    
    if (inputMessageDelayCount > -1) {
      inputMessageDelayCount++;
      if (inputMessageDelayCount > 150) {
        setShowInputChatMessageTextField(false);
        inputMessageDelayCount = -1;
      }
    }
  }
  
  public void detroy() {
    GameGlobal.systemCanvas.getTimer().removeTarget(this);
    if (chatTextField != null) {
      chatTextField.detroy();
      chatTextField = null;
    }
  }
}
